CREATE TABLE TXN(ACTID INT, ACTNAME VARCHAR(10), TXNID INT, TNXA DECIMAL(10,2), TXNDATE DATETIME)
INSERT INTO  TXN VALUES(1, 'ABC', 1, 10, '12/13/2012')

INSERT INTO  TXN VALUES(1, 'ABC', 4, 20, '12/12/2012')

INSERT INTO  TXN VALUES(1, 'ABC', 2, 20, '1/1/2013')
INSERT INTO  TXN VALUES(2, 'PQR', 3, 30, '2/2/2013')

------------------------------------------------------------

DECLARE @DateStart DATETIME,  @DateEnd DATETIME, @StartDate DATETIME, @EndDate DATETIME
SET @DateStart = '12/12/2012' SET @DateEnd = '1/1/2013'
SELECT @StartDate =  DATEADD(MM, 0, DATEADD(MM, DATEDIFF(MM, 0, @DateStart), 0))
SELECT @EndDate = DATEADD(MM, 0, DATEADD(MS, -2, DATEADD(MM, 1, DATEADD(MM, DATEDIFF(MM, 0, @DateEnd), 0))))

DECLARE @TblMonthRange TABLE (SrNo INT, MonthName VARCHAR(20), MonthNumber INT)
;WITH CTETALLY AS (SELECT TOP (	DATEDIFF(mm, DATEADD(mm, DATEDIFF(mm, 0, CAST(@StartDate AS VARCHAR)),0),  
								DATEADD(mm, DATEDIFF(mm, 0, CAST(@EndDate AS VARCHAR)) + 1, 0))) 
								SrNo = ROW_NUMBER() OVER (ORDER BY T1.OBJECT_ID)
								FROM Master.SYS.ALL_COLUMNS T1
								CROSS JOIN Master.SYS.ALL_COLUMNS T2)

INSERT INTO @TblMonthRange
SELECT  SrNo, MonthName = SUBSTRING(DATENAME(MONTH,DATEADD(mm, DATEDIFF(mm, 0, CAST(@DateStart AS VARCHAR)) + T.SrNo - 1,0)), 1, 3),
				MonthNumber = MONTH(DATEADD(mm, DATEDIFF(mm, 0, CAST(@DateStart AS VARCHAR)) + T.SrNo - 1, 0)) FROM CTETALLY T
--SELECT * FROM @TblMonthRange
CREATE TABLE #TblMonthData (AccountID INT, AccountName VARCHAR(50), MonthNumber INT, TxnCount INT, TNXA DECIMAL(10,2))
INSERT INTO #TblMonthData
SELECT ACTID, ACTNAME, MONTH(TXNDATE) AS MonthNumber, ISNULL(COUNT(TXNID), 0) AS TxnCount, ISNULL(SUM(TNXA), 0.00) AS TNXA
FROM TXN WHERE TXNDATE BETWEEN @StartDate AND @EndDate		
GROUP BY ACTID, ACTNAME, MONTH(TXNDATE)
--SELECT * FROM #TblMonthData
CREATE TABLE #ResultMonthData (AccountID INT, ACCOUNTNAME VARCHAR(50), MonthName VARCHAR(5), TxnCount INT, TNXA DECIMAL(10,2))
INSERT INTO #ResultMonthData
SELECT A.AccountID, A.AccountName, B.MonthName, A.TxnCount, A.TNXA
FROM #TblMonthData A JOIN @TblMonthRange B ON B.MonthNumber = A.MonthNumber
--SELECT * FROM #ResultMonthData

DECLARE @SqlQuery NVARCHAR(MAX) 
DECLARE @ImmediateAccountTypeName VARCHAR(1)
SET @ImmediateAccountTypeName = 'A'
SET @SqlQuery = ''
CREATE TABLE #TblMonth (SrNo INT, MonthName VARCHAR(5))
INSERT INTO #TblMonth
SELECT SrNo, MonthName FROM @TblMonthRange
SET @SqlQuery = 'SELECT '  + STUFF( (SELECT ', ''' + MonthName + '''  AS ' + 'COL' + CAST(SrNo AS VARCHAR) + '' FROM #TblMonth FOR XML PATH('')),1,2,'')
EXECUTE SP_EXECUTESQL @SqlQuery
DROP TABLE #TblMonth

DECLARE @SQL NVARCHAR(MAX) 
SET @SQL = ''
SELECT @SQL = @SQL + ', MIN (CASE WHEN [MonthName] = ' + QUOTENAME(MonthName,'''') + '
					 THEN TxnCount END) AS  TxnCount_' + MonthName + 
					 ', MIN (CASE WHEN [MonthName] = ' + QUOTENAME(MonthName,'''') + '
					 THEN TNXA END) AS  TNXA_' + MonthName 
					 FROM @TblMonthRange ORDER BY SrNo

SET @SQL = 'SELECT [ACCOUNTNAME] ' + @SQL +  ' FROM #ResultMonthData GROUP BY [AccountID], [ACCOUNTNAME]  ORDER BY [ACCOUNTNAME]'
PRINT @SQL	 
EXECUTE SP_EXECUTESQL @SQL

DROP TABLE #TblMonthData
DROP TABLE #ResultMonthData